package com.ruoyi.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;

import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;

/**
 * 参数配置内部Controller，实现远程feign接口com.ruoyi.system.api.RemoteSysConfigService。
 * 
 * @author ruoyi
 * @date 2023-04-26
 */
@RestController
@RequestMapping("/inner/config")
public class SysConfigInnerController
{
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询参数配置列表
     */
    @InnerAuth
    @PostMapping("/list")
    public R<List<SysConfig>> selectSysConfigList(@RequestBody SysConfig sysConfig)
    {
        List<SysConfig> list = sysConfigService.selectConfigList(sysConfig);
        return R.ok(list);
    }

    /**
     * 获取参数配置详细信息
     */
    @InnerAuth
    @GetMapping(value = "/{configId}")
    public R<SysConfig> selectSysConfigByConfigId(@PathVariable("configId") Long configId)
    {
        SysConfig sysConfig = sysConfigService.selectConfigById(configId);
        return R.ok(sysConfig);
    }

    /**
     * 新增参数配置
     */
    @InnerAuth
    @PostMapping
    public R<Integer> insertSysConfig(@RequestBody SysConfig sysConfig)
    {
        Integer result = sysConfigService.insertConfig(sysConfig);
        return R.ok(result);
    }

    /**
     * 修改参数配置
     */
    @InnerAuth
    @PutMapping
    public R<Integer> updateSysConfig(@RequestBody SysConfig sysConfig)
    {
        Integer result = sysConfigService.updateConfig(sysConfig);
        return R.ok(result);
    }

    /**
     * 批量删除参数配置
     */
	@InnerAuth
	@DeleteMapping("/{configIds}")
    public R<Integer> deleteSysConfigByConfigIds(@PathVariable Long[] configIds)
    {
        sysConfigService.deleteConfigByIds(configIds);
        return R.ok(configIds.length);
    }

    /**
     * 刷新参数缓存
     */
    @InnerAuth
    @DeleteMapping("/refreshCache")
    public R<Integer> refreshCache()
    {
        sysConfigService.resetConfigCache();
        return R.ok(1);
    }

    /**
     * 根据配置key查询参数配置的配置值
     */
    @InnerAuth
    @GetMapping("/value")
    public R<String> selectConfigByKey(String configKey)
    {
        String value = sysConfigService.selectConfigByKey(configKey);
        return R.ok(value);
    }
}
